<?php
/* 
* [文章管理控制器]
* @author: mazhenyu[houdunwangmzy@163.com]
* @Date:   2014-06-18 17:20:11
* @Last Modified time: 2014-06-24 11:01:23
*/
class ArticleControl extends CommonControl{
	private $model;

	public function __auto(){
		$this->model = K('Article');
	}
	/**
	 * [index 文章列表]
	 * @return [type] [description]
	 */
	public function index(){
		$this->_get_arc(0);
		$this->display();
	}

	/**
	 * [add 添加文章]
	 */
	public function add(){
		if(IS_POST){
			//如果自动验证不通过
			if(!$this->model->create()) $this->error($this->model->error);
			//执行模型添加
			if(!$this->model->add_data()) $this->error($this->model->error);
			$this->success('添加成功',U('index'));
		}
		
		
		$this->display();
	}

	/**
	 * [edit 编辑]
	 * @return [type] [description]
	 */
	public function edit(){
	$id_ar_db =K('Article')->get_ar_db_by_id(array('aid'=>intval($_GET['aid'])));
	$this->assign('id_ar_db',$id_ar_db);
	$cate_all = K('Category')->get_all(array('cid','cname'));
	$this->assign('cate_all',$cate_all);
	// var_dump($id_ar_db);
	// die;
	
		$this->display();
	}

	/**
	 * [del 删除到回收站]
	 * @return [type] [description]
	 */
	public function del(){
		$aid = Q('get.aid',0,'intval');
		$this->model->del_back($aid,1);
		$this->success('删除成功');
	}

	/**
	 * [true_del 真正的删除]
	 * @return [type] [description]
	 */
	public function true_del(){
		$aid = Q('get.aid',0,'intval');
		$this->model->true_del($aid);
		$this->success('删除成功');
	}

	/**
	 * [recycle 回收站]
	 * @return [type] [description]
	 */
	public function recycle(){
		$this->_get_arc(1);
		$this->display();
	}


	/**
	 * [get_back 还原]
	 * @return [type] [description]
	 */
	public function get_back(){
		$aid = Q('get.aid',0,'intval');
		$this->model->del_back($aid,0);
		$this->success('还原成功');
	}


	/**
	 * [_get_arc 获得文章数据包括分页]
	 * @param  [type] $isRecycle [description]
	 * @return [type]            [description]
	 */
	private function _get_arc($isRecycle){
		//所取字段
		$field = 'aid,cid,title,attr,sendtime,updatetime,cname,author';
		//where条件
		$where = array('is_recycle'	=>$isRecycle);
		//查找相对应分类的文章
		$cid = Q('get.cid',0,'intval');
		if($cid) $where['category_cid'] = $cid;
		//查找文章
		$data = K('ArticleCateView')->get_all($where,$field);
		//取得分页信息
		$page = $data['page'];
		$this->assign('page',$page);
		//防止循环出错
		unset($data['page']);
		$this->assign('data',$data);
	}
}
?>
